package com.thinkingcoder.tccache.core.command.zset;

import static com.thinkingcoder.tccache.constant.CommandConstants.ZSetCmd.ZADD;

import java.util.Arrays;

import com.thinkingcoder.tccache.core.TcCache;
import com.thinkingcoder.tccache.core.command.Command;
import com.thinkingcoder.tccache.core.model.Reply;

/**
 * ZADD command.
 */
public class ZAddCmd implements Command {
    @Override
    public String name() {
        return ZADD;
    }

    @Override
    public Reply<?> exec(TcCache cache, String[] args) {
        String key = getKey(args);
        String[] scores = getHKeys(args);
        String[] vals = getHValues(args);
        return Reply.integer(cache.zAdd(key, vals, toDouble(scores)));
    }

    private double[] toDouble(String[] scores) {
        return Arrays.stream(scores).mapToDouble(Double::parseDouble).toArray();
    }
}
